﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

/// <summary>
/// 继承MonoBehaviour的单例方式(加上DontDestroyOnLoad(),不主动销毁不会被销毁)
/// </summary>
public class GameManage :MonoBehaviour
{
    private static GameManage self;

    static GameObject tempObj;

    public bool isAutoCollect = false;
    public int frameFreq = 180;

    public static GameManage getInstance()
    {
        if (self == null)
        {
            tempObj = new GameObject("TempAdd");
            self = tempObj.AddComponent("GameManage") as GameManage;
            DontDestroyOnLoad(tempObj);
        }
        return self;
    }

    void Update()
    {
        //if (!isAutoCollect) return;

        if(Time.frameCount % frameFreq == 0)
        {
            SelfDestroy();
            System.GC.Collect();
        }
    }

    public void SelfDestroy()
    {
        Destroy(tempObj);
        tempObj = null; 
        self = null;
    }


}